package com.extension.company.view.makemoney;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseActivity;
import com.extension.company.common.utils.DisplayUtils;
import com.extension.company.common.utils.Utils;
import com.extension.company.contract.makemoney.SellerPageContract;
import com.extension.company.presenter.makemoney.SellerPagePrestener;
import com.extension.company.view.income.FragmentSPage1;
import com.extension.company.view.income.FragmentSPage2;
import com.extension.company.common.widget.AppBarStateChangeListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;

public class SellerPage extends BaseActivity<SellerPageContract.View, SellerPagePrestener>
        implements SellerPageContract.View, View.OnClickListener {

    @BindView(R.id.m_back)
    ImageView m_back;

    @BindView(R.id.m_top)
    LinearLayout m_top;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.layout_search)
    LinearLayout layout_search;

    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;

    @BindViews({R.id.tv_tag1, R.id.tv_tag2})
    List<TextView> tags;

    private List<Fragment> listFragments;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, SellerPage.class);
        activity.startActivity(intent);
    }

    @Override
    protected void initData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
            layoutParams.height = Utils.getStatusBarHeight(this);
            m_top.setLayoutParams(layoutParams);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    |View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            m_top.setBackgroundColor(getResources().getColor(R.color.white));
            //设置toolbar的高度
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
            layoutParams.height = DisplayUtils.convertDip2Px(this, 44)
                    + Utils.getStatusBarHeight(this);
            toolbar.setLayoutParams(layoutParams);
        }
        listFragments = new ArrayList<Fragment>();
        FragmentSPage1 fragmentSPage1 = new FragmentSPage1();
        FragmentSPage2 fragmentSPage2 = new FragmentSPage2();
        listFragments.add(fragmentSPage1);
        listFragments.add(fragmentSPage2);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        m_top.setAlpha(0.0f);
    }

    @Override
    protected void initListener() {
        m_back.setOnClickListener(this);
        tags.get(0).setOnClickListener(this);
        tags.get(1).setOnClickListener(this);
        setOnPageChangeListener();
        appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if( state == State.EXPANDED ) {
                    //展开状态
//                    m_back.setImageResource(R.drawable.fanh_white);
//                    layout_search.setBackgroundResource(R.drawable.search_back3);
                }else if(state == State.COLLAPSED){
                    //折叠状态
//                    m_back.setImageResource(R.drawable.fanh);
//                    layout_search.setBackgroundResource(R.drawable.search_back_griy);
                }else {
                    //中间状态
//                    m_back.setImageResource(R.drawable.fanh_white);
//                    layout_search.setBackgroundResource(R.drawable.search_back3);
                }
            }
        });
    }

    @Override
    public int getMainLayout() {
        return R.layout.sellerpage;
    }

    @Override
    public SellerPageContract.View createView() {
        return this;
    }

    @Override
    public SellerPagePrestener createPresenter() {
        return new SellerPagePrestener();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.m_back:
                finish();
                break;
            case R.id.tv_tag1:
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv_tag2:
                viewpager.setCurrentItem(1);
                break;
        }
    }

    private void setPagerStatus(int temp) {
        tags.get(0).setTextColor(getResources().getColor(R.color.tvc23));
        tags.get(1).setTextColor(getResources().getColor(R.color.tvc23));
        if (temp == 0) {
            tags.get(0).setTextColor(getResources().getColor(R.color.topcolor));
        }else if (temp == 1) {
            tags.get(1).setTextColor(getResources().getColor(R.color.topcolor));
        }
    }

    private void setOnPageChangeListener() {
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                setPagerStatus(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    //适配器
    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return listFragments.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listFragments.size();
        }
    }
}
